<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">客户分类</p>
    </div>
    <div class="view-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="info"
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-table v-if="refreshTable" v-loading="loading" :data="userList" row-key="sortId"
            :default-expand-all="isExpandAll"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column prop="sortName" label="分类名称" width="150">
              <template slot-scope="scope">
                <span @click="handleItem(scope.row)" style="cursor:pointer">{{ scope.row.sortName }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="sortCode" label="分类编码" align="center"></el-table-column>
          </el-table>
        </el-col>
        <el-col :span="1">
          <div class="line"></div>
        </el-col>
        <el-col :span="15">
          <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item prop="searchValue">
              <el-input
                v-model="queryParams.searchValue"
                placeholder="名称或者编码模糊查询"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="loading" :data="customerList" @row-click="handleView" :default-sort="{prop: 'date', order: 'descending'}" border>
            <el-table-column label="序号" type="index" align="center" width="50">
              <template slot-scope="scope">
                <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
              </template>
            </el-table-column>
            <el-table-column label="编码" align="center" prop="code" :show-overflow-tooltip="true" width="150" sortable />
            <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true" />
            <!-- <el-table-column label="简称" align="center" prop="abbreviation"  :show-overflow-tooltip="true" /> -->
            <!-- <el-table-column label="销售区域" align="center" prop="salesAreaName" :show-overflow-tooltip="true" /> -->
            <el-table-column label="币种" align="center" prop="currency" :show-overflow-tooltip="true" />
            <el-table-column label="联系人" align="center" prop="contact" :show-overflow-tooltip="true" />
            <!-- <el-table-column label="付款条件" align="center" prop="paymentTerms" :show-overflow-tooltip="true" /> -->
            <el-table-column label="创建者" align="center" prop="createBy" />
            <el-table-column value-format="yyyy-MM-dd" label="创建日期" align="center" prop="createTime" width="180px" />
            <el-table-column label="审核状态" align="center" prop="approvalStatus">
              <template slot-scope="scope">
                <div v-if="scope.row.approvalStatus == 0" style="color:#13C2C2">草稿</div>
                <div v-if="scope.row.approvalStatus == 1" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.approvalStatus == 2" style="color:#52C41A">已审核</div>
                <div v-if="scope.row.approvalStatus == 3" style="color:#fd562c">拒绝</div>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="scope">
                <div v-if="scope.row.status == 0" style="color:#13C2C2">禁用</div>
                <div v-if="scope.row.status == 1" style="color:#52C41A">正常</div>
              </template>
            </el-table-column>
            <el-table-column label="最后更新" align="center" prop="updateBy" />
            <el-table-column value-format="yyyy-MM-dd" label="更新日期" align="center" prop="updateTime" width="180px" />
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="handleItem()"
          />
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { listUser, listUserList } from "@/api/system/basicdata/basicdata"

export default {
  name: "sortList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格树数据
      userList: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 客户列表
      customerList: [],
      // 查询参数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerSortId: '',
        // approvalStatus: 2,
        // status: 1,
        searchValue: undefined
      },
      type: '',
      id: '',
      data: ''
    }
  },
  created() {
    if(this.$route.query.type){
      this.type = this.$route.query.type
      this.id = this.$route.query.id
      this.data = this.$route.query.data
    }
    this.getList()
    this.handleItem()
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      listUserList().then(res => {
        this.userList = this.handleTree(res.data, "sortId")
        this.loading = false
      })
    },
    // 转换客户分类数据结构 
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.sortId,
        label: node.sortName,
        children: node.children
      }
    },
    // 展开/折叠
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    // 根据分类查看客户
    handleItem(item){
      if(item !== undefined){
        this.queryParams.customerSortId = item.sortId
      }
      listUser(this.addDateRange(this.queryParams)).then(res => {
        this.customerList = res.rows
        this.total = res.total
      })
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.handleItem()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 查看详情
    handleView(row) {
      if(this.type == 'salesOrderAdd'){
        if(row.status == '1' && row.approvalStatus == "2"){
          if(this.id){
            this.$tab.closeOpenPage({path:"/sales/salesOrder/add",query:{ user: row, data: this.data, id: this.id }})
          }else{
            this.$tab.closeOpenPage({path:"/sales/salesOrder/add",query:{ user: row, data: this.data }})
          }
        }else{
          this.$modal.msgError('请选择已审核的客户！')
        }
      }else if(this.type == 'salesOrderEdit'){
        if(row.status == '1' && row.approvalStatus == "2"){
          this.$tab.closeOpenPage({path:"/sales/salesOrder/edit",query:{ user: row, data: this.data, id: this.id }})
        }else{
          this.$modal.msgError('请选择已审核的客户！')
        }
      }else{
        this.$router.push({path:"/customer/user/detail",query:{id: row.id, type: 'user'}})
      }
    }
  }
}
</script>
